package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.factorydata.domain.MaterialCategory;
import com.ruoyi.factorydata.domain.vo.MaterialCategoryTreeVo;
import com.ruoyi.factorydata.domain.vo.ProcessCategoryTreeVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.ProcessCategory;
import com.ruoyi.factorydata.service.IProcessCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工序分类Controller
 * 
 * @author ruoyi
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/processcategory/processcategory")
public class ProcessCategoryController extends BaseController
{
    @Autowired
    private IProcessCategoryService processCategoryService;

    /**
     * 查询工序分类列表
     */
    @PreAuthorize("@ss.hasPermi('processcategory:processcategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessCategory processCategory)
    {
        startPage();
        List<ProcessCategory> list = processCategoryService.selectProcessCategoryList(processCategory);
        return getDataTable(list);
    }

    /**
     * 导出工序分类列表
     */
    @PreAuthorize("@ss.hasPermi('processcategory:processcategory:export')")
    @Log(title = "工序分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProcessCategory processCategory)
    {
        List<ProcessCategory> list = processCategoryService.selectProcessCategoryList(processCategory);
        ExcelUtil<ProcessCategory> util = new ExcelUtil<ProcessCategory>(ProcessCategory.class);
        util.exportExcel(response, list, "工序分类数据");
    }

    /**
     * 获取工序分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('processcategory:processcategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(processCategoryService.selectProcessCategoryById(id));
    }

    /**
     * 新增工序分类
     */
    @PreAuthorize("@ss.hasPermi('processcategory:processcategory:add')")
    @Log(title = "工序分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcessCategory processCategory)
    {
        boolean exist = processCategoryService.lambdaQuery().eq(ProcessCategory::getCategoryCode, processCategory.getCategoryCode()).count() > 0;
        if ( exist){
            return warn("工序分类编码已存在");
        }
        processCategory.setCreateBy(getUserId().toString());
        return toAjax(processCategoryService.insertProcessCategory(processCategory));
    }

    /**
     * 修改工序分类
     */
    @PreAuthorize("@ss.hasPermi('processcategory:processcategory:edit')")
    @Log(title = "工序分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcessCategory processCategory)
    {
        boolean exist = processCategoryService.lambdaQuery()
                .eq(ProcessCategory::getCategoryCode, processCategory.getCategoryCode())
                .ne(ProcessCategory::getId, processCategory.getId()).count() > 0;
        if ( exist){
            return warn("工序分类编码已存在");
        }
        return toAjax(processCategoryService.updateProcessCategory(processCategory));
    }

    /**
     * 删除工序分类
     */
    @PreAuthorize("@ss.hasPermi('processcategory:processcategory:remove')")
    @Log(title = "工序分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(processCategoryService.deleteProcessCategoryByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('processcategory:processcategory:tree')")
    @GetMapping("/tree")
    public AjaxResult tree(ProcessCategory processCategory)
    {
        List<ProcessCategoryTreeVo> materialCategoryTree = processCategoryService.getProcessCategoryTree(processCategory);
        return success(materialCategoryTree);
    }
}
